﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Threading;
using System.Xml.Serialization;

namespace SQLDeployTool
{

	public class XmlLogger
	{
		#region Public Enumerations
		/// <summary>
		/// Used for log file
		/// </summary>
		public enum Status
		{
			Success,
			Error
		}

		#endregion

		#region private fields

		private string m_ScriptName;
		private DateTime m_beginProcessing;
		private DateTime m_endProcessing;
		private string m_scriptMessage;
		private Status m_scriptStatus;
		private XmlDocument m_xDoc;
		private StringBuilder m_logfile;
		private string m_dbName;
		private string m_dbServer;
		
		#endregion
		
		#region public fields
		public string ScriptName
		{
			get
			{
				return m_ScriptName;
			}
			set
			{
				m_ScriptName = value;
			}
		}
		public DateTime BeginProcessing
		{
			get
			{
				return m_beginProcessing;
			}
			set
			{
				m_beginProcessing = value;
			}
		}
		public DateTime EndProcessing
		{
			get
			{
				return m_endProcessing;
			}
			set
			{
				m_endProcessing = value;
			}
		}
		public string ScriptMessage
		{
			get
			{
				return m_scriptMessage;
			}
			set
			{
				m_scriptMessage = value;
			}
		}

		public Status ScriptStatus 
		{
			get
			{
				return m_scriptStatus;
			}
			set
			{
				m_scriptStatus = value;
			}
		}

		public string Database
		{
			get
			{
				return m_dbName;
			}
			set
			{
				m_dbName = value;
			}
		}

		public string Server
		{
			get
			{
				return m_dbServer;
			}
			set
			{
				m_dbServer = value;
			}
		}

		#endregion

		#region static methods
		
		public static void SchemaReadError(Object sender, ValidationEventArgs e)
		{
			string error = e.Message;
		}

		#endregion

		#region public methods
		
		public XmlLogger()
		{
			InitFile();
			InitDoc();
		}
		
		public void Update()
		{
			try
			{
				m_xDoc.Load(m_logfile.ToString());
			}
			catch
			{
				//throw;
			}


			XmlNode scriptsNode = m_xDoc.CreateElement("Scripts");
			try
			{
				m_xDoc.AppendChild(scriptsNode);
			}
			catch
			{
				//throw;
			}
			scriptsNode = m_xDoc.SelectSingleNode("Scripts");

			XmlNode scriptNode = m_xDoc.CreateElement("Script");
			scriptsNode.AppendChild(scriptNode);

			XmlNode serverNode = m_xDoc.CreateElement("Server");
			serverNode.AppendChild(m_xDoc.CreateTextNode(m_dbServer));
			scriptNode.AppendChild(serverNode);
			
			XmlNode databaseNode = m_xDoc.CreateElement("Database");
			databaseNode.AppendChild(m_xDoc.CreateTextNode(m_dbName));
			scriptNode.AppendChild(databaseNode);

			XmlNode nameNode = m_xDoc.CreateElement("Name");
			nameNode.AppendChild(m_xDoc.CreateTextNode(m_ScriptName));
			scriptNode.AppendChild(nameNode);

			XmlNode beginProcessingNode = m_xDoc.CreateElement("BeginProcessing");
			beginProcessingNode.AppendChild(m_xDoc.CreateTextNode(m_beginProcessing.ToString()));
			scriptNode.AppendChild(beginProcessingNode);

			XmlNode endProcessingNode = m_xDoc.CreateElement("EndProcessing");
			endProcessingNode.AppendChild(m_xDoc.CreateTextNode(m_endProcessing.ToString()));
			scriptNode.AppendChild(endProcessingNode);

			XmlNode statusNode = m_xDoc.CreateElement("Status");
			statusNode.AppendChild(m_xDoc.CreateTextNode(m_scriptStatus.ToString()));
			scriptNode.AppendChild(statusNode);

			XmlNode messageNode = m_xDoc.CreateElement("Message");
			messageNode.AppendChild(m_xDoc.CreateTextNode(m_scriptMessage));
			scriptNode.AppendChild(messageNode);

			this.SaveFile();

		}

		#endregion

		#region private methods
		
		private void InitFile()
		{
			if ( m_logfile == null )
			{
				m_logfile = new StringBuilder();

				m_logfile.Append(DateTime.Now.Month.ToString());
				m_logfile.Append("-");
				m_logfile.Append(DateTime.Now.Day.ToString());
				m_logfile.Append("-");
				m_logfile.Append(DateTime.Now.Year.ToString());
				m_logfile.Append("_");
				m_logfile.Append(DateTime.Now.Hour.ToString());
				m_logfile.Append(".");
				m_logfile.Append(DateTime.Now.Minute.ToString());
				m_logfile.Append(".");
				m_logfile.Append(DateTime.Now.Second.ToString());
				m_logfile.Append(".xml");
			}
			

		}

		private void InitDoc()
		{
			m_xDoc = new XmlDocument();
			ValidationEventHandler SchemaEventHandler = new ValidationEventHandler(SchemaReadError);
			//FileStream fs = new FileStream(@"Xml\Logger.xsd", FileMode.Open);
			//XmlSchema xSchema = XmlSchema.Read(fs, new ValidationEventHandler(SchemaEventHandler));
			//xSchema.Compile(SchemaEventHandler);

			//m_xDoc.Schemas.Add(xSchema);

			XmlNode docNode = m_xDoc.CreateXmlDeclaration("1.0", "UTF-8",null);
			m_xDoc.AppendChild(docNode);
		}
		
		private void SaveFile()
		{
			XmlWriter writer = new XmlTextWriter(m_logfile.ToString(), null);
			m_xDoc.Save(writer);
			writer.Close();
		}


		#endregion
		
	}

}
